package com.xln.furns.entity;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Set;

public class Cart {
//包含多个CartItem,使用HashMap来保存
    private HashMap<Integer,CartItem> items = new HashMap<>();

    public HashMap<Integer, CartItem> getItems() {
        return items;
    }
    public void updateCount(int count,int id){
        CartItem cartItem = items.get(id);
        if (cartItem != null){
            cartItem.setCount(count);
            cartItem.setTotalPrice(cartItem.getPrice().multiply(new BigDecimal(cartItem.getCount())));

        }

    }
    public void delItem(int id){

        items.remove(id);
    }

    public void clearCart(){
        items.clear();
    }



    public BigDecimal getallTotalPrice(){
        BigDecimal allTotalPrice;
        allTotalPrice = new BigDecimal(0);
        Set<Integer> keys = items.keySet();
        for (Integer id : keys) {
            allTotalPrice = allTotalPrice.add(items.get(id).getTotalPrice());
        }
        return allTotalPrice;
    }

    public Integer getTotalFurnCount(){
        Integer totalFurnCount;
        totalFurnCount = 0;
        Set<Integer> keys = items.keySet();
        for (Integer id : keys) {
            totalFurnCount++;

        }
        return totalFurnCount;
    }

    public void addItem(CartItem cartItem){

       CartItem item = items.get(cartItem.getId());
        if (item == null){
            item = cartItem;
            items.put(cartItem.getId(),item);

        }else {
            item.setTotalPrice(item.getTotalPrice().add(cartItem.getTotalPrice()));

            item.setCount((item.getCount()+1));

        }

    }
    public int getTotalCount(){
        Integer totalCount = 0;
        Set<Integer> keys = items.keySet();
        for (Integer id : keys) {
            totalCount += items.get(id).getCount();

        }

        return totalCount;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "items=" + items +
                '}';
    }
}
